package com.ymttest.common.order.userquery.util;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.bson.types.ObjectId;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Duration;
import org.joda.time.Instant;
import org.joda.time.Period;

/**
 * @author tuwenjie 2016年5月7日 下午4:17:39
 */
public class Utils {

	public static String uuid() {
		return new ObjectId().toHexString();
	}

	/**
	 * 处理null问题
	 *
	 * @return
	 */
	public static BigDecimal zeroIfNull(BigDecimal number) {
		BigDecimal zero = BigDecimal.ZERO;
		if (number == null)
			number = zero;

		return number;
	}
	/**
	 * 处理null问题
	 *
	 * @return
	 */
	public static Double zeroIfNull(Double number) {
		Double zero = 0D;
		if (number == null)
			number = zero;

		return number;
	}

	/**
	 * 处理null问题
	 *
	 * @return
	 */
	public static Integer zeroIfNull(Integer number) {
		Integer zero = 0;
		//return optional(number, zero);

		if (number == null)
			number = zero;

		return number;
	}
	/**
	 * 处理null问题
	 *
	 * @return
	 */
	public static Float zeroIfNull(Float number) {
		Float zero = 0F;
		if (number == null)
			number = zero;

		return number;
	}

	/**
	 * 处理null问题
	 *
	 * @return
	 */
	public static Long zeroIfNull(Long number) {
		Long zero = 0L;
		if (number == null)
			number = zero;

		return number;
	}

	/**
	 * 处理null问题
	 *
	 * @param b
	 * @return
	 */
	public static boolean falseIfNull(Boolean b) {
		boolean number = true;
		if (b == null)
			number = false;

		return number;
	}

	public static String trimAndLeft(String src, int n) {
		return StringUtils.left(StringUtils.trim(src), n);
	}

	public static Date minDateIfNull(Date date) {
		return date != null ? date : new Date(0, 0, 1);
	}

	public static Date maxDateIfNull(Date date) {
		return date != null ? date : new DateTime(9999, 12, 31, 23, 59, 59)
				.toDate();
	}

	public static Date toDay(Date date) {
		Date result = DateUtils.setHours(date, 0);
		result = DateUtils.setMinutes(result, 0);
		result = DateUtils.setSeconds(result, 0);
		result = DateUtils.setMilliseconds(result, 0);
		return result;
	}

	public static int daysBetween(Date beginDate, Date endDate) {
		return Days.daysBetween(new DateTime(toDay(beginDate)),
				new DateTime(toDay(endDate))).getDays();
	}

	public static String trim(String source, char dest) {
		char[] value = source.toCharArray();
		int len = value.length;
		int st = 0;
		char[] val = value; /* avoid getfield opcode */

		while ((st < len) && (val[st] == dest)) {
			st++;
		}
		while ((st < len) && (val[len - 1] == dest)) {
			len--;
		}
		return ((st > 0) || (len < value.length))
				? source.substring(st, len)
				: source;
	}

	public static String convertDotNetTimeSpanForHour(int hour) {
		int day = hour / 24;
		int mod = hour % 24;

		return (day > 0 ? (day + ".") : "") + fixZero(mod) + ":00:00";
	}

	public static String convertDotNetTimeSpanForDay(int day) {
		return day + ".00:00:00";
	}

	/**
	 * @param config 00.00:00:00 格式
	 * @return
	 */
	public static Period convertPeriodFromString(String config) {
		String temp = config.trim();

		String[] splits = temp.split("[.]");

		int days = Integer.valueOf(splits[0]);

		String[] times = splits[1].split("[:]");
		int hours = Integer.valueOf(times[0]);
		int minutes = Integer.valueOf(times[1]);
		int seconds = Integer.valueOf(times[2]);
		return new Period(0, 0, 0, days, hours, minutes, seconds, 0);
	}

	/**
	 * 算出两时间之前的差值
	 * @param start
	 * @param end
	 * @return
	 */
	public static String convertDotNetTimeSpanPeriod(DateTime start,
			DateTime end) {
		Period period = new Period(start, end);
		return convertDotNetTimeSpanPeriod(period);
	}

	public static String convertDotNetTimeSpanPeriod(Period period) {
		StringBuffer sb = new StringBuffer();

		if (period.getDays() > 0) {
			sb.append(period.getDays()).append(".");
		}
		sb.append(fixZero(period.getHours())).append(":")
				.append(fixZero(period.getMinutes())).append(":")
				.append(fixZero(period.getSeconds()));

		return sb.toString();
	}

	public static String fixZero(int time) {
		if (time >= 10) {
			return time + "";
		} else if (time > 0) {
			return "0" + time;
		} else {
			return "00";
		}
	}

	/**
	 * 比较period 第一个大 return true
	 * @param p1
	 * @param p2
	 * @return
	 */
	public static boolean isLonger(Period p1, Period p2) {
		Instant now = Instant.now();
		Duration d1 = p1.toDurationTo(now);
		Duration d2 = p2.toDurationTo(now);
		return d1.isLongerThan(d2);
	}

}
